package com.hengtiansoft.lfy.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;
import com.hengtiansoft.lfy.entity.ServiceResult;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.utils.Utils;


/**
 * Created by daiye on 2016/10/27 0027.
 */

public class RegisterMobileActivity extends BaseActivity {
    private static final String TAG = RegisterMobileActivity.class.getSimpleName();
    private ImageView iv_back = null;
    private TextView mTvSkip;
    private EditText mEdtMobile;
    private Button mRegisterBt;

    private String mUsername;
    private String mPassword;
    private String mMobileStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initData(){
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if(bundle!=null){
                mUsername = bundle.getString("UserName");
                mPassword = bundle.getString("Password");
            }
        }
    }

    private void initView() {
        setContentView(R.layout.activity_register_mobile);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            public final void onClick(View v) {
                finish();
            }
        });
        mEdtMobile = (EditText) findViewById(R.id.edt_mobile);
        mTvSkip =  (TextView) findViewById(R.id.tv_skip);
        mTvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRegisterBt = (Button)findViewById(R.id.register_mobile_bt);
        mRegisterBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager inputManager = (InputMethodManager) mRegisterBt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(mRegisterBt, 0);
                processRegisterMobile();
            }
        });
    }

    /**
     * 验证手机号码
     */
    private void processRegisterMobile() {
        mMobileStr =  mEdtMobile.getText().toString();
        if(TextUtils.isEmpty(mMobileStr)){
            Toast.makeText(this, R.string.register_mobile_error, Toast.LENGTH_SHORT).show();
            return;
        }
        if(!Utils.isMobileNum(mMobileStr)){
            Toast.makeText(this, R.string.register_mobile_length_error, Toast.LENGTH_SHORT).show();
            return;
        }

        registerMobile();
    }

    private void registerMobile(){
        NetHelper.modifyUserMobile( RegisterMobileActivity.this, mUsername, mMobileStr, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if (rspData.getRetcode() == 0) {
                    HXPreferenceUtils.getInstance()
                            .setCurrentUserMobile(mMobileStr);
                    Toast.makeText(RegisterMobileActivity.this, rspData.getMessage(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    RegisterMobileActivity.this.finish();
                } else {
                    Toast.makeText(RegisterMobileActivity.this, rspData.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(RegisterMobileActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, ServiceResult.class);
    }
}
